/*******************************************************************************

 * Copyright 2011 See AUTHORS file.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.badlogicgames.superjumper;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.Sprite;

public class Turret extends DynamicGameObject {
	// width is larger than height because the screen is in landscape mode.
	public static final float WIDTH = 2.32f;
	public static final float HEIGHT = 1;
	public static final float ORIGIN_X = WIDTH * 0.25f;
	public static final float ORIGIN_Y = HEIGHT / 2;
	public float rotation = 0f;
	float stateTime = 0;
	public boolean clockwise = true;
	public boolean direction = true;
    public final Random rand;
    public List<Bullet> bullets = new ArrayList<Bullet>();
    public float lastStateTime=0;

	public Turret(float x, float y) {
		super(x, y-10/32f, WIDTH, HEIGHT);   //y is fixed with adjustment.
		rand = new Random();
		
		rotation=rand.nextInt(100)+30;
		if (rand.nextFloat() > 0.5f)
		{
			direction=false;
		}
		
	}

	public void update(float deltaTime) {

		if (direction) {
			rotation = rotation + (float)10.0*deltaTime;

		} else {
			rotation = rotation - (float)10.0*deltaTime;

		}

		if (rotation <= 30) {
			direction = true;
		}
		if (rotation >= 150) {
			direction = false;
		}
		this.lastStateTime += deltaTime;
		if(lastStateTime>1){
			Bullet b = new Bullet(this.position.x,this.position.y, this.rotation);
			bullets.add(b);
			lastStateTime=0;
		}
		for(int i=0; i< this.bullets.size(); i++){
			this.bullets.get(i).update(deltaTime);
		}

		stateTime += deltaTime;
	}
}
